//
//  NotificationView.m
//  app公告弹窗 View - Notificación
//
//  Created by wxp on 2025/3/7.
//

#import "NotificationView.h"

@implementation NotificationView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
        [self addSubView];
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)addSubView
{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.contentScrollView];
    [self.contentScrollView addSubview:self.labelContent];
    [self.viewBack addSubview:self.btnClose];
    
    [self resetViewWithContent:@""];
}

#pragma mark - lazy
- (UIView *)viewBack
{
    if (_viewBack == nil) {
        _viewBack = [UIView  new];
        _viewBack.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
    }
    return  _viewBack;
}
- (UILabel *)labelTitle
{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel  new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.font = [UIFont systemFontOfSize:18];
    }
    return  _labelTitle;
}
- (UIScrollView *)contentScrollView
{
    if (_contentScrollView == nil) {
        _contentScrollView = [UIScrollView  new];
        _contentScrollView.backgroundColor = COLOR_WHITE;
        _contentScrollView.userInteractionEnabled = true;
        _contentScrollView.showsVerticalScrollIndicator = false;
    }
    return  _contentScrollView;
}
- (UILabel *)labelContent
{
    if (_labelContent == nil) {
        _labelContent = [UILabel  new];
        _labelContent.fontNum = 12;
        _labelContent.textColor = COLOR_CONTENT;
        _labelContent.numberOfLines = 0;
        _labelContent.lineSpace = 3;
    }
    return  _labelContent;
}
- (UIButton *)btnClose
{
    if (_btnClose == nil) {
        _btnClose = [UIButton  new];
        _btnClose.backgroundColor = COLOR_PRIMARY;
        _btnClose.titleLabel.fontNum = 14;
        [_btnClose setTitle:@"Entiendo" forState:UIControlStateNormal];
        [_btnClose setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnClose.titleLabel.font = [UIFont systemFontOfSize:18];
        _btnClose.tag = 1;
        [_btnClose addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnClose;
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    switch (sender.tag) {
        case 1:
        {
            [self removeFromSuperview];
        }
            break;
            
        default:
            break;
    }
}


#pragma mark - 刷新View
- (void)resetViewWithContent:(NSString *)content{
    self.viewBack.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    
    [self.labelTitle fitTitle:@"Notificación" variable:0];
    self.labelTitle.centerXTop = XY(self.viewBack.width/2, Ratio(50));
    
    self.contentScrollView.width = self.viewBack.width;
    
    // 显示富文本内容
    self.labelContent.width = self.viewBack.width-30*2;
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *options = @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding) };
    NSError *error;
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:&error];
    NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
    // 设置字体大小
    UIFont *font = [UIFont systemFontOfSize:12];
    [mutableAttributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, mutableAttributedString.length)];
    // 设置行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;  // 设置行间距
    [mutableAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mutableAttributedString.length)];
    
    self.labelContent.attributedText = mutableAttributedString;
    self.labelContent.lineBreakMode = NSLineBreakByWordWrapping;
    [self.labelContent sizeToFit];
    
    if(self.labelContent.height <= Ratio(100)){
        self.contentScrollView.height = self.labelContent.height;
    }else{
        self.contentScrollView.contentSize = CGSizeMake(0, self.labelContent.height);
        self.contentScrollView.height = Ratio(100);
    }
    self.contentScrollView.leftTop = XY(0, self.labelTitle.bottom+(26));
    self.labelContent.centerXTop = XY(self.contentScrollView.width/2, 0);
    
    self.btnClose.widthHeight = XY(self.viewBack.width-30*2, BUTTON_HEIGHT);
    [GlobalMethod setRoundView:self.btnClose color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
    self.btnClose.leftTop = XY(30, self.contentScrollView.bottom+(26));
    
    self.viewBack.height = self.btnClose.bottom+30;
    self.viewBack.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
}


@end
